Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

[repo].tsx

49 lines1.3 KB

1import React from 'react'
2import Link from 'next/link'
3import { useQuery } from '@tanstack/react-query'
4import { usePathname } from 'next/navigation'
5​
6async function fetchProject(id: string): Promise<{
7 forks_count: number
8 stargazers_count: number
9 watchers_count: number
10}> {
11 console.info('Fetching project:', id)
12​
13 const response = await fetch(`https://api.github.com/repos/${id}`)
14 if (!response.ok) throw new Error('Failed to fetch repo')
15 await new Promise((r) => setTimeout(r, 1000))
16 return await response.json()
18​
19export default function Repo() {
20 const id = usePathname()
21​
22 const { status, data, error, isFetching } = useQuery({
23 queryKey: ['team', id],
24 queryFn: () => fetchProject(id),
25 })
26​
27 return (
28 <div style={{ textAlign: 'center' }}>
29 <h1>{id}</h1>
30 {status === 'pending' ? (
31 'Loading...'
32 ) : status === 'error' ? (
33 <span>Error: {error.message}</span>
34 ) : (
35 <>
36 <div>
37 <p>forks: {data.forks_count}</p>
38 <p>stars: {data.stargazers_count}</p>
39 <p>watchers: {data.watchers_count}</p>
40 </div>
41 <div>{isFetching ? 'Background Updating...' : ' '}</div>
42 </>
43 )}
44 <br />
45 <br />
46 <Link href="/">Back</Link>
47 </div>
48 )